package com.mswyt.utils.test.jdbc;

import com.mswyt.utils.msDataSource.MsDataSourceUtils;
import com.mswyt.utils.msDataSource.dto.MsDataSourceMsgDto;
import com.mswyt.utils.test.pb.mapper.MsDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: MsDataSourceSwitch
 * @Description: 数据源切换
 */
@Component
public class MsDataSourceSwitch {
    @Autowired
    private MsDictMapper mapper;

    public List<Map<String, Object>> switchDataSource() throws Exception {
        //使用默认的数据源查询
        List<Map<String, Object>> b1List = mapper.b1();


        //切换到b2： 10.44.2.3 数据源
        List<Map<String, Object>> b2List = mapper.b2();


        //模拟：在请求时候，需要从默认数据库A的t表获取一个数据源Db3，然后动态去加载该数据源
        //把db3信息加入到MsDataSourceMsgDto对象
        MsDataSourceMsgDto db3 = new MsDataSourceMsgDto(
                "jdbc:mysql://10.88.15.26:3306/empoworx_nwyw_business?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true",
                "root",
                "root");
        //把数据源db3进行注册即可
        MsDataSourceUtils.addDataSource("db3", db3);
        List<Map<String, Object>> b3List = mapper.b3();

        //使用默认数据源查询
        List<Map<String, Object>> b11List = mapper.b1();
        b1List.addAll(b2List);
        b1List.addAll(b3List);
        b1List.addAll(b11List);
        return b1List;
    }

}
